/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import modelo.Disciplina;
import modelo.Turma;

/**
 *
 * @author Caps
 */
@Stateless
public class DisciplinaFacade extends AbstractFacade<Disciplina> {

    @PersistenceContext(unitName = "SagrauPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DisciplinaFacade() {
        super(Disciplina.class);
    }

    public List<Turma> carregarTurmas(String dscDescricao) {
        TypedQuery<Disciplina> q = getEntityManager().createQuery("select d from Disciplina d where d.dscDescricao =:arg", Disciplina.class).setParameter("arg", dscDescricao);
        q.setMaxResults(1);

        List<Disciplina> lista = q.getResultList();
        if (lista == null) {
            return null;
        }
        
        List<Turma> turmas = null;
        for (int i = 0; i < lista.size(); i++) {           
            turmas = lista.get(i).getTurmaList();           
        }

        return turmas;

    }
    
        public List<Disciplina> select(String dscDisciplina){

        TypedQuery<Disciplina> q = getEntityManager().createQuery(
                "select d from Disciplina d where d.dscDescricao =:arg1", Disciplina.class);
                q.setParameter("arg1", dscDisciplina);

        q.setMaxResults(1);

        List<Disciplina> lista = q.getResultList();
        if (lista == null) {
            return null;
        }

        return lista;
    }
}
